/**
 * 
 */
package fr.paulmasson.jfreex.ui;

import java.awt.Canvas;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.graphics.Color;

import uk.co.caprica.vlcj.player.MediaPlayer;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.DefaultFullScreenStrategy;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.player.embedded.FullScreenStrategy;
import uk.co.caprica.vlcj.player.events.VideoOutputEventListener;
import fr.paulmasson.jfreex.JFreeX;

/**
 * @author Paul MASSON : contact@paulmasson.fr
 * @version 11.7.8
 */
public class VlcPlayer {

	private static final long serialVersionUID = 1L;
	
	private static MediaPlayerFactory mediaPlayerFactory;
	private static EmbeddedMediaPlayer mediaPlayer;

	
	public VlcPlayer(Frame frame, final JFreeX jFreeXSWT, final Color color) {
		
		
		final Canvas videoSurface = new Canvas();
		
		
		
		videoSurface.setBackground(new java.awt.Color(color.getRed(), color.getGreen(), color.getBlue()));
		
		


		List<String> vlcArgs = new ArrayList<String>();

		vlcArgs.add("--no-plugins-cache");
		vlcArgs.add("--no-video-title-show");
		vlcArgs.add("--no-snapshot-preview");

		FullScreenStrategy fullScreenStrategy = new DefaultFullScreenStrategy(frame);

		mediaPlayerFactory = new MediaPlayerFactory(vlcArgs.toArray(new String[vlcArgs.size()]));
		//		mediaPlayerFactory.setUserAgent("JFreeTV");

		mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer(fullScreenStrategy);
		mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(videoSurface));
		mediaPlayer.setPlaySubItems(true);
		

		frame.add(videoSurface);
		
		mediaPlayer.addVideoOutputEventListener(new VideoOutputListener());
		
		
		videoSurface.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount()==2) {
					jFreeXSWT.fullscreen();
					if (jFreeXSWT.isFullscreen()) {
						videoSurface.setBackground(java.awt.Color.BLACK);
					} else {
						videoSurface.setBackground(new java.awt.Color(color.getRed(), color.getGreen(), color.getBlue()));
					}
				}
			}
		});
	}
	
	public void playMedia(String url) {
		mediaPlayer.playMedia(url);
	}
	
	
	private final class VideoOutputListener implements VideoOutputEventListener {

		public void videoOutputAvailable(MediaPlayer player, boolean videoOutput) {
			
		}
	}


	public void stopMedia() {
		mediaPlayer.stop();
		mediaPlayer.playMedia(this.getClass().getResource("/data/splash.jpg").toString().replaceAll("file:",""));
	}
	
	public void pauseMedia() {
		mediaPlayer.pause();
	}
	
	public void playMedia() {
		mediaPlayer.play();
	}

	public void setVolume(int vol) {
		mediaPlayer.setVolume(vol);
	}
	
	
	
}
